package com.filmatchs.filmatchs;

import com.filmatchs.R;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.TextUtils;

public class MovieProvider extends ContentProvider {

	private static final int URI_MOVIES = 1;
	private static final int URI_MOVIE_ID = 2;

	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI("com.filmatchs.provider.filmatchs", "movies",
				URI_MOVIES);
		uriMatcher.addURI("com.filmatchs.provider.filmatchs", "movies/#",
				URI_MOVIE_ID);
	}

	private SQLiteDatabase movieDB;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		Context context = getContext();

		MovieDatabaseHelper dbHelper = new MovieDatabaseHelper(context,
				getContext().getResources().getString(R.string.DATABASE_NAME),
				null, getContext().getResources().getInteger(
						R.integer.DATABASE_VERSION));

		movieDB = dbHelper.getWritableDatabase();

		return (movieDB == null) ? false : true;

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

		qb.setTables(getContext().getResources().getString(
				R.string.TABLE_MOVIES));

		switch (uriMatcher.match(uri)) {
		case URI_MOVIE_ID:
			qb.appendWhere("");
			break;

		default:
			break;
		}

		String orderBy;
		if (TextUtils.isEmpty(sortOrder)) {
			orderBy = getContext().getResources().getString(
					R.string.KEY_MOVDATE);
		} else {
			orderBy = sortOrder;
		}

		Cursor c = qb.query(movieDB, projection, selection, selectionArgs,
				null, null, sortOrder);

		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	class MovieDatabaseHelper extends SQLiteOpenHelper {

		private static final String DATABASE_CREATE = "";

		public MovieDatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}

	}

}
